package com.iwinding.ladygg.net.callback;

import android.app.Activity;
import android.content.Intent;

import com.iwinding.ladygg.constant.SPConstant;
import com.iwinding.ladygg.net.response.BaseRopnse;
import com.iwinding.ladygg.okhttputils.callback.Callback;
import com.iwinding.ladygg.ui.act.GuideActivity;
import com.iwinding.ladygg.ui.act.LoginActivity;
import com.iwinding.ladygg.utils.GsonUtil;
import com.iwinding.ladygg.utils.LogUtil;
import com.iwinding.ladygg.utils.SPUtils;
import com.yayandroid.theactivitymanager.TheActivityManager;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hezhiyong on 2017/3/24.
 */

public abstract class BaseRpCallback<T> extends Callback<BaseRopnse> {

    public static final String UNKNOWN_ACCESS_TOKEN = "1000";
    public static final String UNKNOWN_SESSION = "1030";
    public static final String UNKNOWN_USER_SESSION = "1031";


    @Override
    public BaseRopnse parseNetworkResponse(Response response, int id) throws Exception {
        String jsonStr = response.body().string();
        LogUtil.i("@hzy", "icane----BaseRpCallback:" + jsonStr);
        BaseRopnse baseRopnse =
                GsonUtil.getInstance().formJson(jsonStr, BaseRopnse.class);
        return baseRopnse;
    }

    @Override
    public void onResponse(BaseRopnse response, int id) {
        if (!response.success) {
            onFail(response.errMsg);
            boolean isTokenExpired = isTokenExpired(response);
            boolean isCookieNull = isCookieNull(response);
            Activity mCurrentActivity =TheActivityManager.getInstance().getCurrentActivity();
            if (isTokenExpired) {
                SPUtils.putSpProperty(mCurrentActivity, SPConstant.IS_TOKEN_EXPIRED, true);
            }
            if (isCookieNull) {
                SPUtils.putSpProperty(mCurrentActivity, SPConstant.COOKIE, "");
            }
            LogUtil.i("@hzy", "icane----isTokenExpired:" + isTokenExpired);
            LogUtil.i("@hzy", "icane----isCookieNull:" + isCookieNull);
            if (isTokenExpired || isCookieNull) {
                TheActivityManager.getInstance().setAsLanding(mCurrentActivity);
                if (mCurrentActivity != null && !(mCurrentActivity instanceof GuideActivity)) {
                    LogUtil.i("@hzy","----------BaseRpCallback");
                    TheActivityManager.getInstance().toLanding();
                    Intent intent = new Intent(mCurrentActivity, GuideActivity.class);
                    mCurrentActivity.startActivity(intent);
                    TheActivityManager.getInstance().finishInstance(mCurrentActivity.getClass());
                }
            }
            return;
        }
        try {
            T t = parseResponse(response, id);
            onResult(t);
        }catch (Exception e){
            onFail("解析失敗");
        }
    }

    public abstract T parseResponse(BaseRopnse response, int id);

    public abstract void onResult(T t);

    public abstract void onFail(String errorMsg);

    @Override
    public void onError(Call call, Exception e, int id) {

    }

    /**
     * 根据Response，判断Token是否失效
     *
     * @param response
     * @return
     */
    private boolean isTokenExpired(BaseRopnse response) {
        String errcode = response.errCode;
        boolean isExpired = false;
        switch (errcode) {
            case UNKNOWN_ACCESS_TOKEN:
                isExpired = true;
                break;
        }
        return isExpired;
    }

    private boolean isCookieNull(BaseRopnse response) {
        String errcode = response.errCode;
        boolean isCookieNull = false;
        switch (errcode) {
            case UNKNOWN_SESSION:
            case UNKNOWN_USER_SESSION:
                isCookieNull = true;
                break;
        }
        return isCookieNull;
    }

}